Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic

Imports Framework


'Table-Row Class (Customisable half)
Partial Public Class CLink

#Region "Constants"
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc as CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    
    'Hidden (UI code should use cache)
    Protected Friend Sub New(linkFromItemId As Integer, linkToItemId As Integer)
        MyBase.New(linkFromItemId, linkToItemId)
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, linkFromItemId As Integer, linkToItemId As Integer)
        MyBase.New(dataSrc, linkFromItemId, linkToItemId)
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal linkFromItemId As Integer, linkToItemId As Integer, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, linkFromItemId, linkToItemId, txOrNull)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Custom()
        'm_sampleDateCreated = DateTime.Now
        'm_sampleSortOrder   = 0
    End Sub
#End Region

#Region "Default Connection String"
    Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Properties - Relationships"    
    'Relationships - Foriegn Keys (e.g parent)
    Public ReadOnly Property From() As CItem
        Get
            Return CItem.Cache.GetById(Me.LinkFromItemId)
        End Get
    End Property
    Public ReadOnly Property [To]() As CItem
        Get
            Return CItem.Cache.GetById(Me.LinkToItemId)
        End Get
    End Property

    'Relationships - Collections (e.g. children)

#End Region

#Region "Properties - Customisation"
    'Derived/ReadOnly (e.g. xml classes, presentation logic)
    
#End Region

#Region "Save/Delete Overrides"
     'Can Override MyBase.Save/Delete (e.g. Cascade deletes, or insert related records)
#End Region

#Region "Custom Database Queries"
    '(Not normally required for cached classes, use list class for searching etc)
    'For Stored Procs can use: MakeListTyped (matching schema), or DataSrc.ExecuteDataset (reports etc)
    'For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
#End Region



#Region "Caching Details"
    'Cache Key
    Friend Shared CACHE_KEY As String = GetType(CLink).ToString 'TABLE_NAME

    'Cache data
    Private Shared Function LoadCache() As CLinkList
        Return New CLink().SelectAll()
    End Function
    
    'Cache Timeout
    Private Shared Sub SetCache(ByVal value As CLinkList)
        If Not IsNothing(value) Then value.Sort()
        CCache.Set(CACHE_KEY, value) 'Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
    End Sub
    
    'Helper Method
    Private Function CacheGetById(ByVal list As CLinkList) As CLink
        Return list.GetById(Me.LinkFromItemId, Me.LinkToItemId)
    End Function
#End Region

#Region "Cloning"
    Public Function Clone(target as CDataSrc, txOrNull as IDbTransaction) As CLink ', parentId As Integer) As CLink
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CLink(Me, target)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        copy.Save(txOrNull)

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.LinkFromItemId)

        Return copy
    End Function
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region

End Class
